cc.Class({
    extends: cc.Component,

    properties: {
    },

    onLoad () {
        this.node.cascadeOpacity = false;
        this.node.on('touchstart', event => {
            this.hidePop();
            event.stopPropagation();
        }, this);
        this.pop = this.node.find('pop');
        this.pop.setScale(0);

        var container = this.pop.find('container');
        container.find('btnHelp').on('click', () => {
            this.onClick('HelpPop');
            cc.Proxy.newburyingPoint(EventConfig.getEventId('游戏说明'));
        }, this);
        this.lb_detail = container.find('btnHelp').getComponent('cc.Label');
        container.find('btnMotors').on('click', () => {
            this.onClick('MotorsPop');
            cc.Proxy.newburyingPoint(EventConfig.getEventId('摩托介绍'));
        }, this);
        this.lb_intro = container.find('btnMotors').getComponent('cc.Label');
        container.find('btnRecord').on('click', () => {
            this.onClick('RecordPop');
            cc.Proxy.newburyingPoint(EventConfig.getEventId('支持记录'),'支持记录');
        }, this);
        this.lb_record = container.find('btnRecord').getComponent('cc.Label');
        container.find('btnService').on('click', () => this.showServicePop(), this);
        this.lb_service = container.find('btnService').getComponent('cc.Label');
        container.find('btnHome').on('click', () => {
            cc.GameProxy.backHome();
            cc.Proxy.newburyingPoint(EventConfig.getEventId('返回大厅'));
        }, this);
        this.lb_back = container.find('btnHome').getComponent('cc.Label');
        this.btnSound = container.find('btnSound');
        this.lb_music = this.btnSound.getComponent('cc.Label');
        this.btnSound.on('click', this.toggleSound, this);

        this.initWords();
    },
    initWords: function(){
        this.lb_detail.string = LangLocal.word1;
        this.lb_intro.string = LangLocal.word2;
        this.lb_record.string = LangLocal.word3;
        this.lb_service.string = LangLocal.word4;
        this.lb_music.string = LangLocal.word5;
        this.lb_back.string = LangLocal.word6;
    },
    start() {
        var txt = LangLocal.word5;
        if(!cc.Sound.isSoundOff()) {
            txt = LangLocal.word7;
            cc.Sound.resumeAll();
        }
        this.btnSound.getComponent(cc.Label).string = txt;
    },

    toggleSound() {
        cc.Sound.toggleSound();
        cc.Proxy.newburyingPoint(EventConfig.getEventId('关闭音乐'));
        var txt = cc.Sound.isMute ? LangLocal.word5 : LangLocal.word7;
        this.btnSound.getComponent(cc.Label).string = txt;
    },

    showServicePop() {
        this.hidePop();
        cc.Popup.show('ServicePop');
    },

    onClick(popName) {
        this.hidePop();
        
        cc.Popup.show(popName);
    },

    showPop() {
        if(this.pop.scaleX > 0) return;
        this.node.x = 0;
        this.pop.stopAllActions();
        this.pop.runAction(cc.scaleTo(0.3, 1).easing(cc.easeBackOut()));
    },

    hidePop() {
        if(this.pop.scaleX < 1) return;
        this.pop.stopAllActions();
        var s = cc.scaleTo(0.2, 0).easing(cc.easeSineIn());
        this.pop.runAction(cc.sequence(s, cc.callFunc(() => this.node.x = 10000, this)));
    }
});
